package com.bzr.emos.wx.controller;


import com.alibaba.fastjson2.JSONObject;
import com.bzr.emos.wx.Service.LoginService;
import com.bzr.emos.wx.Service.UserService;
import com.bzr.emos.wx.common.util.R;
import com.bzr.emos.wx.config.security.LoginDTO;
import com.bzr.emos.wx.db.pojo.WechatUsers;
import com.bzr.emos.wx.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/verify")
public class VerifyActivationController {

    @Autowired
    UserService userService;
    @Autowired
    private LoginService loginService;










    //验证授权码
    @RequestMapping("/activation")
    private R activation(@RequestBody Map<String, String> request) {


        if (request.get("register_code") != null || !request.get("register_code").isEmpty()) {
            //如果输入的值是 000000 时，验证是否存在管理员账号
            if (request.get("register_code").equals("000000")) {
                if (!userService.checkOpenId()) {
                    return R.ok().put("role", "0");
                } else {
                    return R.error();
                }
            } else {
                if (userService.checkPassword(request.get("register_code"))) {
                    return R.ok();
                } else {
                    return R.error();
                }
            }
        }
        return R.error();
    }



    //验证微信登录
    @RequestMapping("/VerifyLogin")
    private R VerifyLogin(@RequestBody Map<String, String> request) {
        JSONObject openJson = userService.getOpenId(request.get("code"));
        if (openJson != null) {
            WechatUsers wechatUser = userService.checkWechatUser(openJson.getString("openid"));
            if (wechatUser != null) {

               String token= JwtUtils.generateToken(wechatUser.getId().toString());
                return R.ok().put("data", wechatUser).put("token", token);
            }
        }

            return R.error();
    }



    //测试请求

    @RequestMapping("/test")
    private R TEST(@RequestBody Map<String, String> request) {
        log.info("body:{}",request.get("body").toString());
        return R.ok().put("code", "23456");
    }



}
